package net.lab1024.sa.user.module.business.points.service;

import net.lab1024.sa.common.common.domain.PageParam;
import net.lab1024.sa.common.common.domain.PageResult;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.common.util.SmartRequestUtil;
import net.lab1024.sa.common.module.support.jdunion.JdUnionService;
import net.lab1024.sa.common.module.support.jdunion.constant.OrderStatus;
import net.lab1024.sa.common.module.support.jdunion.domain.vo.OrderInfoVO;
import net.lab1024.sa.user.module.business.points.constant.PointsChangeStatus;
import net.lab1024.sa.user.module.business.points.constant.PointsChangeType;
import net.lab1024.sa.user.module.business.points.domain.form.ApplyPointsForm;
import net.lab1024.sa.user.module.business.points.domain.form.PointsChangeRecordAddForm;
import net.lab1024.sa.user.module.business.points.domain.form.PointsChangeRecordQueryForm;
import net.lab1024.sa.user.module.business.points.domain.vo.PointsBalanceVO;
import net.lab1024.sa.user.module.business.points.domain.vo.PointsChangeRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class PointsService {

    @Autowired
    private JdUnionService jdUnionService;


    @Autowired
    private PointsChangeRecordService pointsChangeRecordService;

    public ResponseDTO<String> applyPoints(ApplyPointsForm applyPointsForm){

        Long orderId = applyPointsForm.getOrderId();

        ResponseDTO<OrderInfoVO> orderInfoRsp = this.jdUnionService.getOrderInfo(orderId);

        if(!orderInfoRsp.getOk()){
            return ResponseDTO.userErrorParam(orderInfoRsp.getMsg());
        }

        if(orderInfoRsp.getData().getOrderStatus().equals(OrderStatus.INVALID)){
            return ResponseDTO.userErrorParam("订单状态无效");
        }

        ResponseDTO<String> checkOrderIdRsp=pointsChangeRecordService.checkOrderId(orderId);
        if(!checkOrderIdRsp.getOk()){
            return ResponseDTO.userErrorParam(checkOrderIdRsp.getMsg());
        }


        PointsChangeRecordAddForm pointsChangeRecordAddForm=new PointsChangeRecordAddForm();

        pointsChangeRecordAddForm.setUserId(SmartRequestUtil.getRequestUserId());
        pointsChangeRecordAddForm.setCourseId(0);
        pointsChangeRecordAddForm.setChangePoints(Integer.valueOf((int)Math.round(orderInfoRsp.getData().getEstimateCosPrice().get())));
        pointsChangeRecordAddForm.setChangeStatus(PointsChangeStatus.PENDING_APPROVAL.getValue());
        pointsChangeRecordAddForm.setChangeType(PointsChangeType.ADD.getValue());
        pointsChangeRecordAddForm.setOrderId(orderId);
        pointsChangeRecordAddForm.setRemark("京东购物");
        ResponseDTO<String> addRsp = pointsChangeRecordService.add(pointsChangeRecordAddForm);
        if(!addRsp.getOk()){
            return ResponseDTO.userErrorParam(addRsp.getMsg());
        }


        return ResponseDTO.ok();
    }


    public ResponseDTO<PointsBalanceVO> getPointsBalance(){

        PointsBalanceVO pointsBalanceVO=new PointsBalanceVO();

        PointsChangeRecordQueryForm pointsChangeRecordQueryForm=new PointsChangeRecordQueryForm();
        pointsChangeRecordQueryForm.setUserId(SmartRequestUtil.getRequestUserId());
        pointsChangeRecordQueryForm.setChangeStatus(new Integer[]{PointsChangeStatus.COMPLETED.getValue()});
        Integer pageNum=1;
        pointsChangeRecordQueryForm.setPageSize(100);
        List<PageParam.SortItem> sortItemList=new ArrayList<PageParam.SortItem>();
        PageParam.SortItem sortItem=new PageParam.SortItem();
        sortItem.setColumn("record_id");
        sortItem.setIsAsc(true);
        sortItemList.add(sortItem);

        pointsChangeRecordQueryForm.setSortItemList(sortItemList);
        PageResult<PointsChangeRecordVO> pointsChangeRecordVOPageResult;

        AtomicReference<Integer> points= new AtomicReference<>(0);
        do{
            pointsChangeRecordQueryForm.setPageNum(pageNum);
            pointsChangeRecordVOPageResult = pointsChangeRecordService.queryPage(pointsChangeRecordQueryForm);

            if(!pointsChangeRecordVOPageResult.getEmptyFlag()){

              pointsChangeRecordVOPageResult.getList().forEach(pointsChangeRecordVO -> {
                  if(pointsChangeRecordVO.getChangeType().equals(PointsChangeType.ADD.getValue())){
                      points.updateAndGet(v -> v + pointsChangeRecordVO.getChangePoints());
                  } else if (pointsChangeRecordVO.getChangeType().equals(PointsChangeType.REDUCE.getValue())) {
                      points.updateAndGet(v->v-pointsChangeRecordVO.getChangePoints());
                  }
              });

            }

            pageNum++;
        }while (!pointsChangeRecordVOPageResult.getEmptyFlag());

        pointsBalanceVO.setPoints(points.get());


        return ResponseDTO.ok(pointsBalanceVO);
    }




}
